on:
  push:
    branches:
      - master
  pull_request:
name: Qa workflow
env:
  extensions: mbstring, intl, iconv, libxml, dom, json, simplexml, zlib, fileinfo, gnupg
  key: cache-v1 # can be any string, change to clear the extension cache.
  defaultPHPVersion: '7.2'
  phiveGPGKeys: 4AA394086372C20A,D2CCAC42F6295E7D,E82B2FB314E9906E,8A03EA3B385DBAA1
  phiveHome: $GITHUB_WORKSPACE/.phive
jobs:
  setup:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Setup cache environment
        id: cache-env
        uses: shivammathur/cache-extensions@v1
        with:
          php-version: ${{ env.defaultPHPVersion }}
          extensions: ${{ env.extensions }}
          key: ${{ env.key }}

      - name: Cache extensions
        uses: actions/cache@v2
        with:
          path: ${{ steps.cache-env.outputs.dir }}
          key: ${{ steps.cache-env.outputs.key }}
          restore-keys: ${{ steps.cache-env.outputs.key }}

      - name: Setup PHP
        uses: shivammathur/setup-php@master
        with:
          php-version: ${{ env.defaultPHPVersion }}
          extensions: ${{ env.extensions }}
          ini-values: memory_limit=2G, display_errors=On, error_reporting=-1
          tools: pecl

      - name: Get composer cache directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: Cache composer dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: composer-

      - name: Install Composer dependencies
        run: |
          composer install --no-progress --prefer-dist --optimize-autoloader

      - name: composer-require-checker
        uses: docker://phpga/composer-require-checker-ga
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          args: check --config-file ./composer-require-config.json composer.json

  phpunit-with-coverage:
    runs-on: ubuntu-latest
    name: Unit tests [7.2 | ubuntu-latest]
    needs: setup
    steps:
    - uses: actions/checkout@v2

    - name: Setup cache environment
      id: cache-env
      uses: shivammathur/cache-extensions@v1
      with:
        php-version: ${{ env.defaultPHPVersion }}
        extensions: ${{ env.extensions }}
        key: ${{ env.key }}

    - name: Cache extensions
      uses: actions/cache@v2
      with:
        path: ${{ steps.cache-env.outputs.dir }}
        key: ${{ steps.cache-env.outputs.key }}
        restore-keys: ${{ steps.cache-env.outputs.key }}

    - name: Setup PHP
      uses: shivammathur/setup-php@master
      with:
        php-version: ${{ env.defaultPHPVersion }}
        extensions: ${{ env.extensions }}
        ini-values: memory_limit=2G, display_errors=On, error_reporting=-1
        tools: pecl, phive

    - name: Get composer cache directory
      id: composer-cache
      run: echo "::set-output name=dir::$(composer config cache-files-dir)"

    - name: Cache composer dependencies
      uses: actions/cache@v2
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: composer-

    - name: Install Composer dependencies
      run: |
        composer install --no-progress --prefer-dist --optimize-autoloader

    - name: Install PHAR dependencies
      env:
        GITHUB_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: phive --no-progress install --copy --trust-gpg-keys ${{ env.phiveGPGKeys }} --force-accept-unsigned phpunit scrutinizer-ci/ocular

    - name: PHPUnit
      uses: docker://phpdoc/phpunit-ga:latest
      with:
        args: --testsuite=unit
    - name: Upload to Scrutinizer
      run: tools/ocular code-coverage:upload --format=php-clover build/logs/clover.xml
    - name: Quick check code coverage level
      run: php tests/coverage-checker.php 70

  codestyle:
    runs-on: ubuntu-latest
    needs:
      - setup
    steps:
      - uses: actions/checkout@v2
      - name: Restore/cache vendor folder
        uses: actions/cache@v2
        with:
          path: vendor
          key: all-build-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            all-build-${{ hashFiles('**/composer.lock') }}
            all-build-
      - name: Code style check
        uses: phpDocumentor/coding-standard@latest
        with:
          args: -s

  phpstan:
    runs-on: ubuntu-latest
    needs:
      - setup
    steps:
      - uses: actions/checkout@v2

      - name: Setup cache environment
        id: cache-env
        uses: shivammathur/cache-extensions@v1
        with:
          php-version: ${{ env.defaultPHPVersion }}
          extensions: ${{ env.extensions }}
          key: ${{ env.key }}

      - name: Cache extensions
        uses: actions/cache@v2
        with:
          path: ${{ steps.cache-env.outputs.dir }}
          key: ${{ steps.cache-env.outputs.key }}
          restore-keys: ${{ steps.cache-env.outputs.key }}

      - name: Setup PHP
        uses: shivammathur/setup-php@master
        with:
          php-version: ${{ env.defaultPHPVersion }}
          extensions: ${{ env.extensions }}
          ini-values: memory_limit=2G, display_errors=On, error_reporting=-1
          tools: pecl

      - name: Get composer cache directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: Cache composer dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: composer-

      - name: Install Composer dependencies
        run: |
          composer install --no-progress --prefer-dist --optimize-autoloader

      - name: PHPStan
        uses: phpDocumentor/phpstan-ga@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          args: analyse src tests --configuration phpstan.neon

  psalm:
    runs-on: ubuntu-latest
    needs: [setup]
    steps:
      - uses: actions/checkout@v2

      - name: Setup cache environment
        id: cache-env
        uses: shivammathur/cache-extensions@v1
        with:
          php-version: ${{ env.defaultPHPVersion }}
          extensions: ${{ env.extensions }}
          key: ${{ env.key }}

      - name: Cache extensions
        uses: actions/cache@v2
        with:
          path: ${{ steps.cache-env.outputs.dir }}
          key: ${{ steps.cache-env.outputs.key }}
          restore-keys: ${{ steps.cache-env.outputs.key }}

      - name: Setup PHP
        uses: shivammathur/setup-php@master
        with:
          php-version: ${{ env.defaultPHPVersion }}
          extensions: ${{ env.extensions }}
          ini-values: memory_limit=2G, display_errors=On, error_reporting=-1
          tools: pecl, psalm

      - name: Get composer cache directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: Cache composer dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: composer-

      - name: Install Composer dependencies
        run: |
          composer install --no-progress --prefer-dist --optimize-autoloader

      - name: Psalm
        run: psalm --output-format=github

  build-phar:
    runs-on: ubuntu-latest
    name: Build PHAR to test with
    needs:
      - setup
    steps:
      - uses: actions/checkout@v2

      - name: Setup cache environment
        id: cache-env
        uses: shivammathur/cache-extensions@v1
        with:
          php-version: ${{ env.defaultPHPVersion }}
          extensions: ${{ env.extensions }}
          key: ${{ env.key }}

      - name: Cache extensions
        uses: actions/cache@v2
        with:
          path: ${{ steps.cache-env.outputs.dir }}
          key: ${{ steps.cache-env.outputs.key }}
          restore-keys: ${{ steps.cache-env.outputs.key }}

      - name: Setup PHP
        uses: shivammathur/setup-php@master
        with:
          php-version: ${{ env.defaultPHPVersion }}
          extensions: ${{ env.extensions }}
          ini-values: memory_limit=2G, display_errors=On, error_reporting=-1
          tools: pecl

      - name: Get composer cache directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: Cache composer dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: composer-

      - name: Install Composer dependencies
        run: |
          composer install --no-progress --prefer-dist --optimize-autoloader

      - name: warm cache
        uses: phpDocumentor/phar-ga@latest
        with:
          args: php bin/console cache:warmup --env=prod
      - name: build phar
        uses: phpDocumentor/phar-ga@latest
        with:
          args: box compile
      - name: Upload PHAR file
        uses: actions/upload-artifact@v2.2.0
        with:
          name: phpDocumentor.phar
          path: build/phpDocumentor.phar

  smoketest:
    runs-on: ubuntu-latest
    name: Smoke test
    needs:
      - build-phar
    steps:
      - uses: actions/checkout@v2
      - name: Restore/cache vendor folder
        uses: actions/cache@v2
        with:
          path: vendor
          key: all-build-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            all-build-${{ hashFiles('**/composer.lock') }}
            all-build-
      - name: Setup PHP
        uses: shivammathur/setup-php@master
        with:
          php-version: '7.2'
          extensions: mbstring, intl, iconv, libxml, dom, json, simplexml, zlib
          ini-values: memory_limit=2G, display_errors=On, error_reporting=-1
          tools: pecl
      - name: Download PHAR file
        uses: actions/download-artifact@v2.0.5
        with:
          name: phpDocumentor.phar
          path: build
      - name: Did the download succeed?
        run: ls -hal build
      - name: Check version
        run: php ./build/phpDocumentor.phar --version
      - name: Run phpDocumentor on itself
        run: php ./build/phpDocumentor.phar -vvv

  e2e:
    runs-on: ubuntu-16.04
    name: E2E tests [ubuntu-latest]
    needs:
      - setup
    steps:
      - uses: actions/checkout@v2

      - name: Setup cache environment
        id: cache-env
        uses: shivammathur/cache-extensions@v1
        with:
          php-version: ${{ env.defaultPHPVersion }}
          extensions: ${{ env.extensions }}
          key: ${{ env.key }}

      - name: Cache extensions
        uses: actions/cache@v2
        with:
          path: ${{ steps.cache-env.outputs.dir }}
          key: ${{ steps.cache-env.outputs.key }}
          restore-keys: ${{ steps.cache-env.outputs.key }}

      - name: Setup PHP
        uses: shivammathur/setup-php@master
        with:
          php-version: ${{ env.defaultPHPVersion }}
          extensions: ${{ env.extensions }}
          ini-values: memory_limit=2G, display_errors=On, error_reporting=-1
          tools: pecl

      - name: Get composer cache directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: Cache composer dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: composer-

      - name: Install Composer dependencies
        run: |
          composer install --no-progress --prefer-dist --optimize-autoloader

      - name: Build example project using Clean template
        run: ./bin/phpdoc --template=clean -vvv --config=data/examples/MariosPizzeria/phpdoc.xml --target=build/clean
      - name: Build example project using Default template
        run: ./bin/phpdoc --template=default -vvv --config=data/examples/MariosPizzeria/phpdoc.xml --target=build/default
      - name: Test templates using Cypress
        uses: cypress-io/github-action@v1

  e2e-matrix:
    runs-on: ${{ matrix.operating-system }}
    strategy:
      matrix:
        php-versions:
          - '7.2'
        operating-system:
          - ubuntu-16.04
          - windows-latest
          - macOS-latest
    name: E2E tests [${{ matrix.operating-system }}]
    needs:
      - e2e
    steps:
      - uses: actions/checkout@v2

      - name: Setup cache environment
        id: cache-env
        uses: shivammathur/cache-extensions@v1
        with:
          php-version: ${{ matrix.php-versions }}
          extensions: ${{ env.extensions }}
          key: ${{ env.key }}

      - name: Cache extensions
        uses: actions/cache@v2
        with:
          path: ${{ steps.cache-env.outputs.dir }}
          key: ${{ steps.cache-env.outputs.key }}
          restore-keys: ${{ steps.cache-env.outputs.key }}

      - name: Setup PHP
        uses: shivammathur/setup-php@master
        with:
          php-version: ${{ matrix.php-versions }}
          extensions: ${{ env.extensions }}
          ini-values: memory_limit=2G, display_errors=On, error_reporting=-1
          tools: pecl

      - name: Get composer cache directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: Cache composer dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: composer-

      - name: Install Composer dependencies
        run: |
          composer install --no-progress --prefer-dist --optimize-autoloader

      - name: Build example project using Clean template
        run: ./bin/phpdoc --template=clean --config=data/examples/MariosPizzeria/phpdoc.xml --target=build/clean
      - name: Build example project using Default template
        run: ./bin/phpdoc --template=default --config=data/examples/MariosPizzeria/phpdoc.xml --target=build/default
      - name: Test templates using Cypress
        uses: cypress-io/github-action@v1

  phpunit:
    runs-on: ${{ matrix.operating-system }}
    strategy:
      matrix:
        operating-system:
          - ubuntu-latest
          - windows-latest
          - macOS-latest
        php-versions:
          - '7.2'
          - '7.3'
          - '7.4'
        exclude: # already covered in pre-test with coverage above
          - operating-system: 'ubuntu-latest'
            php-versions: '7.2'
    name: Unit tests [${{ matrix.php-versions }} | ${{ matrix.operating-system }}]

    needs:
    - phpunit-with-coverage
    - phpstan
    - codestyle
    steps:
    - uses: actions/checkout@v2

    - name: Setup cache environment
      id: cache-env
      uses: shivammathur/cache-extensions@v1
      with:
        php-version: ${{ matrix.php-versions }}
        extensions: ${{ env.extensions }}
        key: ${{ env.key }}

    - name: Cache extensions
      uses: actions/cache@v2
      with:
        path: ${{ steps.cache-env.outputs.dir }}
        key: ${{ steps.cache-env.outputs.key }}
        restore-keys: ${{ steps.cache-env.outputs.key }}

    - name: Setup PHP
      uses: shivammathur/setup-php@master
      with:
        php-version: ${{ matrix.php-versions }}
        extensions: ${{ env.extensions }}
        ini-values: memory_limit=2G, display_errors=On, error_reporting=-1
        tools: pecl, phive

    - name: Get composer cache directory
      id: composer-cache
      run: echo "::set-output name=dir::$(composer config cache-files-dir)"

    - name: Cache composer dependencies
      uses: actions/cache@v2
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: composer-

    - name: Install Composer dependencies
      run: |
        composer install --no-progress --prefer-dist --optimize-autoloader

    - name: Install PHAR dependencies
      env:
        GITHUB_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: phive --no-progress install --copy --trust-gpg-keys ${{ env.phiveGPGKeys }} --force-accept-unsigned phpunit:^8.4

    - name: Run PHPUnit
      run: php tools/phpunit --testsuite=unit,integration

  demo:
    runs-on: ubuntu-latest
    needs:
      - phpunit
    if: github.ref == 'refs/heads/master'
    steps:
      - uses: actions/checkout@v2
      - name: 'phpDoc clean'
        uses: ./ # use this repo as an action
        with:
          template: 'clean'
          target: 'build/clean'
      - name: 'phpDoc default'
        uses: ./ # use this repo as an action
        with:
          template: 'default'
          target: 'build/default'
      - name: 'Fix file permissions'
        run: 'sudo chown $USER ./build'
      - name: 'Copy docker files'
        run: 'cp ./data/demo/* ./build'
      - uses: danielr1996/envsubst-action@1.0.0
        name: 'Insert cache-busting hash into html'
        with:
          input: ./build/index.html.dist
          output: ./build/index.html
      - name: Build
        uses: elgohr/Publish-Docker-Github-Action@master
        env:
          VERSION: '3.0'
        with:
          name: phpdocumentor/phpdocumentor/demo.phpdoc.org
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
          registry: docker.pkg.github.com
          context: build
          buildargs: VERSION
          tags: '3.0'
        id: demo_build
      - name: 'deploy'
        uses: jaapio/keelsh-deploy@1.0.0
        with:
          keelBaseUrl: ${{ secrets.KEEL_BASE_URL }}
          image: 'docker.pkg.github.com/phpdocumentor/phpdocumentor/demo.phpdoc.org'
          tag: ${{ steps.demo_build.outputs.tag }}

#  build-asset-pack:
#    needs:
#    - phpunit
#    runs-on: ubuntu-latest
#    if: github.ref == 'refs/heads/master'
#    steps:
#    - uses: actions/checkout@v2
#    - name: 'Build assets'
#      uses: ./ # use this repo as an action
#      with:
#          template: 'default'
#          target: 'build/api'
#    - name: 'Copy assets'
#      run: |
#          mkdir -p data/asset-builder/css; \
#          mkdir -p data/asset-builder/js; \
#          cp -r build/api/css/base.css data/asset-builder/css/base.css; \
#          cp -r build/api/css/normalize.css data/asset-builder/css/normalize.css; \
#          cp -r build/api/js/search.js data/asset-builder/js/search.js;
#    - name: 'Switch to assets context'
#      run: cd data/asset-builder
#    # Setup .npmrc file to publish to GitHub Packages
#    - uses: actions/setup-node@v2.1.2
#      with:
#          node-version: '12.x'
#          registry-url: 'https://npm.pkg.github.com'
#          scope: '@phpdocumentor'
#    - name: 'Install dependencies'
#      run: |
#        cd data/asset-builder; \
#        npm install
#    - name: 'Bump version'
#      run: |
#        cd data/asset-builder; \
#        npm version preminor --preid=build
#    - name: 'Publish package'
#      run: |
#        cd data/asset-builder; \
#        npm publish --access public
#      env:
#        NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
#        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
#    - name: Push back package version
#      uses: devops-infra/action-commit-push@0.1
#      with:
#        github_token: "${{ secrets.GITHUB_TOKEN }}"
#        commit_message: "[AUTO] Assets package version bump"
